package org.bookie.model;

import java.util.ArrayList;
import java.util.List;

import javax.persistence.CascadeType;
import javax.persistence.Entity;
import javax.persistence.FetchType;
import javax.persistence.JoinColumn;
import javax.persistence.ManyToOne;
import javax.persistence.OneToMany;
import javax.persistence.Table;

import org.hibernate.validator.NotNull;

@Entity
@Table(name="Bets")
public class Bet extends LabeledEntity {
	private static final long serialVersionUID = -2658160492313741298L;
	/** Event this Outcome belongs to */
    @ManyToOne
    @JoinColumn(name="EventID")
    @NotNull
	private Event event;
    
	@ManyToOne
	@JoinColumn(name="SUCCESSFUL_OUTCOME_ID", nullable = true)
	private Outcome successfulOutcome;
	
	/**List of all outcomes*/
	@OneToMany(fetch=FetchType.LAZY, cascade={CascadeType.ALL}, mappedBy="bet")
	private List<Outcome> outcomes = new ArrayList<Outcome>();
	
	protected Bet(){super();}
	
	public Bet(String label){
		setLabel(label);
	}
	
	public Bet(Event event, String label){
		this(label);
		this.event=event;
	}
	
	/**
	 * Adds a {@link Outcome} to the bet
	 * @param outcomes Outcomes to add
	 * @return this Bet
	 */
	public Bet addOutcome(Outcome... outcomes){
		for(Outcome next:outcomes){
			getOutcomes().add(next);
			next.setBet(this);
		}
		return this;
	}
	
	/**
	 * Get the {@link Event} this Bet belongs to
	 * 
	 * @return instance of Event
	 */
	public Event getEvent() {
		return event;
	}

	/**
	 * Set the {@link Event} this Bet belongs to
	 * 
	 * @param event event to set
	 */
	public void setEvent(Event event) {
		this.event = event;
	}
	/**
	 * Get the {@link Outcome}s of the Bet
	 */
	public List<Outcome> getOutcomes() {
		return outcomes;
	}
	
	/**
	 * Get the {@link Outcome} representing the real outcome of the Event
	 * @return instance of Bet
	 */
	public Outcome getSuccessfulOutcome() {
		return successfulOutcome;
	}
	
	/**
	 * Set the {@link Outcome} representing the real outcome of the Event
	 * @param successful Outcome to set
	 */
	public void setSuccessfulOutcome(Outcome successful) {
		this.successfulOutcome = successful;
	}
	
	@Override
	public String toString() {
		return "Bet:" + getLabel() + "[" + getId() + "]";
	}
}
